package com.orange.base.component.biztool.simulateddata.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.orange.base.component.biztool.simulateddata.service.DictInfo;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataEnum;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;

/**
 * 字典
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class DictStrategy extends AbstractSimulatedData {

    private static final ThreadLocal<HashMap<String, List<DictInfo>>> CONTEXT = new ThreadLocal<HashMap<String, List<DictInfo>>>();

    public String getType() {
        return SimulatedDataEnum.DICT.getName();
    }

    public String getCaption() {
        return SimulatedDataEnum.DICT.getCaption();
    }

    @Override
    public Object getValue(SimulatedDataParam param) {
        if (simulatedDataDictService == null) {
            return null;
        }
        List<DictInfo> dictInfoList = null;
        String dictId = param.getDictId();
        if (CONTEXT.get() == null || !CONTEXT.get().containsKey(dictId)) {
            dictInfoList = simulatedDataDictService.getDictInfo(dictId, param.getExtParam());
            if (dictInfoList == null || dictInfoList.isEmpty()) {
                return null;
            } else {
                if (CONTEXT.get() == null) {
                    HashMap<String, List<DictInfo>> dictMap = new HashMap<String, List<DictInfo>>();
                    dictMap.put(dictId, dictInfoList);
                    CONTEXT.set(dictMap);
                } else {
                    CONTEXT.get().put(dictId, dictInfoList);
                }
            }
        } else {
            dictInfoList = CONTEXT.get().get(dictId);
        }

        int min = 1;
        if (!(param.getMin() == null) && !"".equals(param.getMin())) {
            min = Integer.parseInt(param.getMin());
            min = min > dictInfoList.size() ? dictInfoList.size() : min;
        }
        int max = 1;
        if (!(param.getMax() == null) && !"".equals(param.getMax())) {
            max = Integer.parseInt(param.getMax());
            max = max > dictInfoList.size() ? dictInfoList.size() : max;
        }
        int length = getRandomNumber(min, max);
        List<Object> values = new ArrayList<Object>();
        List<DictInfo> dictInfos = new ArrayList<DictInfo>();
        for (DictInfo dictInfo : dictInfoList) {
            DictInfo newDictInfo = new DictInfo();
            newDictInfo.setCode(dictInfo.getCode());
            newDictInfo.setName(dictInfo.getName());
            dictInfos.add(newDictInfo);
        }
        for (int i = 0; i < length; i++) {
            if (dictInfos.isEmpty()) {
                break;
            }
            int index = getRandomNumber(0, dictInfos.size() - 1);
            DictInfo dictInfo = dictInfos.get(index);
            values.add(dictInfo.getCode());
            dictInfos.remove(index);
        }
        if (values.isEmpty()) {
            return null;
        } else if (values.size() == 1) {
            return values.get(0);
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < values.size(); i++) {
                if (i > 0) {
                    sb.append(",");
                }
                sb.append(values.get(i));
            }
            return sb.toString();
        }
    }

}
